﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class TeacherDAO : CoreAbstractDAO<Teacher>
    {
        private AdapterAbstractDAO<Teacher, TeacherSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<Teacher, TeacherUniversity> wrappedAbstractDAOUniversity;
        private TeacherDAOSchool wrappedDAOSchool;
        private TeacherDAOUniversity wrappedDAOUniversity;

        public TeacherDAO(TeacherDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool =
                new AdapterAbstractDAO<Teacher, TeacherSchool>((AbstractDAO<TeacherSchool>)wrappedDAOSchool);
        }

        public TeacherDAO(TeacherDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<Teacher, TeacherUniversity>((AbstractDAO<TeacherUniversity>)wrappedDAOUniversity);
        }

        public Teacher findByLogin(String login)
        {
            Teacher teacher;
            if (wrappedDAOSchool != null)
            {
                teacher = wrappedDAOSchool.findByLogin(login);

            }
            else if (wrappedDAOUniversity != null)
            {
                teacher = wrappedDAOUniversity.findByLogin(login);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }
            return teacher;
        }

        public override void update(Teacher entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }
        }

        public override void save(Teacher entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(Teacher entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }
        }

        public override void delete(Teacher entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }
        }

        public override Teacher getById(int id)
        {
            Teacher entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<Teacher> getAll()
        {
            IList<Teacher> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped TeacherDAOSchool nor TeacherDAOUniversity was initialized");
            }

            return records;
        }
    }
}
